package com.xbongbong.paas.script.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.service.PaasListGroupService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.script.pojo.dto.DeleteRedGroupDTO;
import com.xbongbong.pro.script.pojo.dto.SyncPaymentUserDTO;
import com.xbongbong.pro.script.pojo.dto.UpdatePaymentDataDTO;
import com.xbongbong.pro.script.pojo.dto.UpdatePaymentExplainsDTO;
import com.xbongbong.pro.script.pojo.vo.ScriptVO;
import com.xbongbong.saas.service.RepairExplainService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * 应用模块名称<p>
 * 代码描述<p>
 * Company: 逍邦网络科技有限公司<p>
 *
 * @author chenshan
 * @version v1.0
 * @since 2019-11-22 15:15
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/script/payment")
public class PaymentScriptController {
	@Resource
	private ProBaseConfig proBaseConfig;
	@Resource
	private PaasListGroupService paasListGroupService;
	@Resource
	private RepairExplainService repairExplainService;
	
	@RequestMapping(value = "/deleteRedGroup", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String deleteRedGroup(@RequestBody @Valid DeleteRedGroupDTO deleteRedGroupDTO, BindingResult br) throws XbbException {
		if ( !Objects.equals(proBaseConfig.getXbbCode(),deleteRedGroupDTO.getXbbCode()) ){
			throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006);
		}
		XbbResponse<ScriptVO> response;
		if (br.hasErrors()) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				ScriptVO scriptVO = paasListGroupService.deleteByGroup();
				response = new XbbResponse<>(scriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}
	
	@RequestMapping(value = "/updatePayment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String updatePaymentExplains(@RequestBody @Valid UpdatePaymentExplainsDTO updatePaymentExplainsDTO, BindingResult br) throws XbbException {
		if ( !Objects.equals(proBaseConfig.getXbbCode(),updatePaymentExplainsDTO.getXbbCode()) ){
			throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006);
		}
		XbbResponse<ScriptVO> response;
		if (br.hasErrors()) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				ScriptVO scriptVO = repairExplainService.updatePaymentExplains(updatePaymentExplainsDTO);
				response = new XbbResponse<>(scriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}
	
	@RequestMapping(value = "/scriptPaymentData", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String scriptPaymentData(@RequestBody @Valid UpdatePaymentDataDTO updatePaymentDataDTO, BindingResult br) throws XbbException {
		if ( !Objects.equals(proBaseConfig.getXbbCode(),updatePaymentDataDTO.getXbbCode()) ){
			throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006);
		}
		XbbResponse<ScriptVO> response;
		if (br.hasErrors()) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				ScriptVO scriptVO = repairExplainService.scriptPaymentData(updatePaymentDataDTO);
				response = new XbbResponse<>(scriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}
	/**
	 *
	 * @Description: 同步应收款团队数据，保持和合同团队一致
	 * @param syncPaymentUserDTO
	 * @param br
	 * @return: java.lang.String
	 * @Author: shen.yang
	 * @Date: 2022/7/25  10:37
	*/
	@RequestMapping(value = "/syncPaymentUser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public String syncPaymentUser(@RequestBody @Valid SyncPaymentUserDTO syncPaymentUserDTO, BindingResult br) throws XbbException {
		if ( !Objects.equals(proBaseConfig.getXbbCode(),syncPaymentUserDTO.getXbbCode()) ){
			throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006);
		}
		XbbResponse<ScriptVO> response;
		if (br.hasErrors()) {
			String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
			response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
		} else {
			try {
				ScriptVO scriptVO = repairExplainService.syncPaymentUser(syncPaymentUserDTO);
				response = new XbbResponse<>(scriptVO);
			} catch (XbbException e) {
				response = new XbbResponse<>(e);
			}
		}
		return JSON.toJSONString(response);
	}
}
